package com.qianchang.microfinance.api;

import android.content.Intent;

import com.dm.library.http.DMOKHttpUtils;
import com.dm.library.http.HttpParams;
import com.dm.library.http.base.JsonResult;
import com.dm.library.http.callback.RequestCallBack;
import com.dm.library.utils.ToastUtil;
import com.dm.library.widgets.InputPwdDialog;
import com.qianchang.microfinance.setting.SettingTradPasswordActivity;
import com.qianchang.microfinance.utils.DMConstant;
import com.qianchang.microfinance.view.base.BaseActivity;
import com.qianchang.microfinance.view.dialog.alert.DimengConfirmAlertDialog;
import com.qianchang.microfinance.view.login.ForgetActivityOne;

/**
 * Created by jinbailiang on 2017/9/5.
 * 检测交易密码
 */

public class InputTradPwdApi {

    private static InputPwdDialog inputDialog;

    public static void checkHasTradPwd(final BaseActivity activity, final PwdCallBack pwdCallBack) {
        activity.show();
        HttpParams params = new HttpParams();
        DMOKHttpUtils.getInstance().post(activity.getApplicationContext(), DMConstant.ApiURL.FIND_TRAD_PWD, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(JsonResult<String> response) {
                activity.dismiss();
                if (DMConstant.Code.CODE_OK.equals(response.getCode())) {
                    String info = response.getObject();//0 表示已有支付密码、1 表示没有支付密码
                    if ("0".equals(info)) {
                        showTradPwdDialog(activity, pwdCallBack);
                    } else {
                        DimengConfirmAlertDialog confirmAlertDialog = DimengConfirmAlertDialog.newInstance("", "请先设置支付密码");
                        confirmAlertDialog.setOnDialogClickListener(new DimengConfirmAlertDialog.OnDialogClickListener() {
                            @Override
                            public void agree() {
                                activity.startActivity(SettingTradPasswordActivity.class);
                            }

                            @Override
                            public void cancel() {

                            }
                        });
                        confirmAlertDialog.show(activity.getSupportFragmentManager(), getClass().getSimpleName());

                    }
                } else {
                    ToastUtil.getInstant().show(activity, response.getDescription());
                }
            }

            @Override
            public void onFailure(String msg) {
                activity.dismiss();
                ToastUtil.getInstant().show(activity, msg);// getString(R.string.msg_indentity_code_send_fail));
            }

            @Override
            public void onConnectFailure(String msg) {

            }
        });
    }


    private static void showTradPwdDialog(final BaseActivity activity, final PwdCallBack pwdCallBack) {
        inputDialog = new InputPwdDialog(activity);
        inputDialog.setForgetPwdListener(new InputPwdDialog.ForgetPwdListener() {
            @Override
            public void forgetPwd() {
                Intent intent = new Intent(activity, ForgetActivityOne.class);
                intent.putExtra(SettingTradPasswordActivity.IS_TRAD_PWD, true);
                activity.startActivity(intent);
            }
        });
        inputDialog.setOnInputFinishListener(new InputPwdDialog.OnInputFinishListener() {
            @Override
            public void finish(String password) {
                pwdCallBack.commit(password);
            }
        });
        inputDialog.show();
    }

    public interface PwdCallBack {
        void commit(String password);
    }

    public static void dismiss() {
        if (inputDialog != null) {
            inputDialog.dismiss();
        }
    }
}
